package com.jz.netthings.bloodsources.controller;


import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.application.bodyCheck.bean.MyPage;
import com.jz.netthings.bean.system.JyRole;
import com.jz.netthings.bloodsources.bean.XyLink;
import com.jz.netthings.bloodsources.bean.XyNoPulping;
import com.jz.netthings.bloodsources.bean.XyRemind;
import com.jz.netthings.bloodsources.cache.BlodSourcesCache;
import com.jz.netthings.bloodsources.constant.BloodSourcesConstant;
import com.jz.netthings.bloodsources.service.XyLinkService;
import com.jz.netthings.bloodsources.service.XyNoPulpingService;
import com.jz.netthings.bloodsources.service.XyRemindService;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.support.SimpleCoreResponse;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 血源链接表
----表关系↓
--- 血源公告牌鉴权表
             |
---血源链接表
          ---提醒记录表
          ---未献浆记录表
          ---回访记录表
 
 前端控制器
 * </p>
 *
 * @author 张凡
 * @since 2019-08-03
 */
@RestController
@RequestMapping("/adscm/bloodSources/xyLink")
public class XyLinkController {
    @Autowired
    XyLinkService xyLinkService;
    @Autowired
    XyRemindService xyRemindService;
    @Autowired
    XyNoPulpingService xyNoPulpingService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @RequestMapping("/queryByCondition")
    public SimpleCoreResponse queryByCondition(
                                          HttpServletRequest request,
                                          String providerAddress,
                                          String nurseName,
                                          String providerName,
                                          String providerActiveLevel,
                                          String useVolunteer,
                                          String currentPage,
                                          String pageSize
                                          ) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<JyRole> jyRoles = token.getUser().getRoles();
        String useNurseDeptId = null;
        Integer useNurseId = null;
        if(jyRoles!=null && jyRoles.size()>=1){
            //证明该登录用户有角色信息，则进行其拥有角色遍历查找，查看其是否拥有相关科室主任角色
            Boolean isDirector = false;
            Boolean isMaster = false;
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_MASTER_CODE.equals(jyRole.getRole_code())){     //如果是站长则直接条件查询
                    isMaster = true;
                    break;
                }
            }
            for (JyRole jyRole : jyRoles) {
                if(BloodSourcesConstant.ROLES_DIRECTOR_CODE.equals(jyRole.getRole_code())){  //如果包含科室主任角色
                    isDirector = true;
                    break;
                }
            }
            if(isMaster){
                //如果是站长，就查询所有，查询约束值 useNurseDeptId，useNurseId 则不用赋值
            }else{
                // a.如果当前登录用户拥有   【科室主任角色】 ==》则获取其所属科室ID==》通过其科室ID（userDeptId = token.getUser().getDeptId();） 获取数据表同属其科室（同deptId）的所有血源链接护士的数据，并可以修改
                if(isDirector){
                    Integer userDeptId = token.getUser().getDeptId();  //用户部门Id
                    useNurseDeptId = userDeptId+"";
                }else{
                    // b.如果当前登录用户未拥有 【科室主任角色】 ==》则获取其用户ID(token.getUser().getId())==>>通过其护士Id(useNurseId《==》userId) 进行数据条件拉取
                    Integer userId = token.getUser().getId();
                    useNurseId = userId;
                }
            }
        }else {

        }
        return new SimpleCoreResponse("200","查询成功", xyLinkService.queryByCondition(useNurseDeptId,useNurseId,providerAddress,nurseName,providerName,providerActiveLevel,useVolunteer,null,currentPage,pageSize));
    }
    @RequestMapping("/insertXyLinkInfo")
    public SimpleCoreResponse insertProviderInfo(@RequestBody XyLink xyLink, HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result= xyLinkService.insertXyLinkInfo(xyLink ,token);
        return new SimpleCoreResponse("200","查询成功" ,result);
    }
    @RequestMapping("/updateXyLinkInfo")
    public SimpleCoreResponse updateXyLinkInfo(@RequestBody XyLink xyLink, HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        //获取是否提醒开关值
        String isPulpingMsg = xyLink.getIsPulpingMsg();
        XyNoPulping xyNoPulping = new  XyNoPulping();
        xyNoPulping.setProviderId(Integer.parseInt(xyLink.getLinkProviderId()+""));
        if("否".equals(isPulpingMsg)){   //判断是否进行提醒消息生成
            xyNoPulping.setIsPushMsg(2);
        }else
          if("是".equals(isPulpingMsg)){ //判断是否进行提醒消息生成
              xyNoPulping.setIsPushMsg(1);
          }
          //进行状态同步操作，同步未献浆记录中取消推送提醒状态的约束值 表:=>【xy_no_pulping】列:=>[isPushMsg]
        xyNoPulpingService.updateByProviderId(xyNoPulping);
        Boolean result= xyLinkService.updateXyLinkInfo(xyLink ,token);
        return new SimpleCoreResponse("200","查询成功" ,result);
    }

    @RequestMapping("/testMsg")
    public SimpleCoreResponse testMsg(HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        for (int i=1;i<3;i++){
            XyRemind xyRemind =  new XyRemind();
            xyRemind.setRemindNurseName("护士"+i);    //护士名称
            xyRemind.setRemindProviderId(i*1L); //浆员姓名
            xyRemind.setRemindDate(new Date());                       //提醒时间
            xyRemind.setNextCollDate(new Date());       //下次献浆时间
            BlodSourcesCache.listDataForRemind.add(xyRemind);
        }
        xyRemindService.insertXyRemindInfo(BlodSourcesCache.listDataForRemind,token);
        return new SimpleCoreResponse("200","查询成功" ,"测试成功");
    }
}

